package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.Library;
import com.haven.webrobotserver.service.LibraryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/library")
public class LibraryController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LibraryService libraryservice;

    @PostMapping("/getLibraryList")
    public JSONObject getLibraryList(@RequestBody Map<String, String> params) {
        JSONObject result = new JSONObject();

        String name = params.get("libraryName");
        String type = params.get("libraryType");
        String function = params.get("libraryFunction");


        List<Library> libraryList = libraryservice.getLibraryList(name, type, function);

        String libraryListJsonStr = JSON.toJSONString(libraryList);


        JSONArray libraryJsons = JSONArray.parseArray(libraryListJsonStr);

        if(libraryJsons != null){

            result.put("status",200);
            result.put("msg","succuss");
            result.put("data",libraryJsons);
            return result;
        }
        result.put("status",400);
        result.put("msg","fail");
        return result;
    }
}
